<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="100px" @submit.native.prevent>
      <el-form-item label="年度" prop="year">
        <el-select v-model="pram.year" placeholder="年度" clearable style="width: 100%">
          <el-option
              v-for="(item) in yearList"
              :key="item"
              :label="item"
              :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属单位" prop="departmentId">
        <el-select v-model="pram.departmentId" placeholder="所属单位" clearable style="width: 100%">
          <el-option
              v-for="(item,index) in departmentList"
              :key="index"
              :label="item.unitName"
              :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工作任务" prop="task">
        <el-input v-model="pram.task" type="textarea" :rows="7" placeholder="工作任务" />
      </el-form-item>
      <el-form-item label="进展情况" prop="evolve">
        <el-input v-model="pram.evolve" type="textarea" :rows="7" placeholder="进展情况" />
      </el-form-item>
      <el-form-item label="排序">
        <el-input-number v-model="pram.sort" :min="0"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:work:save','admin:work:info']">{{ isCreate===0?'确定':'更新' }}</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce/index'
import  * as workApi from '@/api/workApi.js'
import * as roleApi from '@/api/role.js'
import {Debounce} from '@/utils/validate'
export default {
  // name: "edit"
  components: { Tinymce },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    allDepartmentList:{
      type:Array
    },
    allYearList:{
      type:Array
    },
  },
  data() {
    return {
      constants:this.$constants,
      pram: {
        year: null,
        departmentId:null,
        task:null,
        evolve:null,
        sort:0,
        id: 0,
      },
      rules: {
        year: [{ required: true, message: '请选择年度', trigger: ['blur', 'change'] }],
        departmentId: [{ required: true, message: '请选择所属单位', trigger: ['blur', 'change'] }],
        task: [{ required: true, message: '请填工作任务', trigger: ['blur'] }],
        evolve: [{ required: true, message: '请填进展情况', trigger: ['blur'] }],
      },
      departmentList:[],
      yearList:[],
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },

    initEditData() {
      this.yearList = this.allYearList
      this.departmentList = this.allDepartmentList

      if (this.isCreate !== 1) return //编辑
      const { departmentId, id, task, evolve, year,sort } = this.editData //通过list中slot-scope=“scope“，scope.$index就是该行的下标，scope.row就是该行的数据所有消息对象，有了这两个参数就可以实现编辑、删除功能（this.editData就是scope.row传入的对象参数）
      this.pram.departmentId = departmentId
      this.pram.task = task
      this.pram.evolve = evolve
      this.pram.year = year
      this.pram.sort = sort
      this.pram.id = id
    },

    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) {
          return
        }
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      workApi.workAdd(this.pram).then(data => {
        this.$message.success('创建重点工作任务成功')
        this.$emit('hideEditDialog')
      })
    },
    handlerEdit() {
      workApi.workAdd(this.pram).then(data => {
        this.$message.success('更新重点工作任务成功')
        this.$emit('hideEditDialog')
      })
    },
  }
}
</script>

<style scoped>
.tips{
  color:#999;
}
.btn-box{
  margin-top:20px;
}
</style>
